"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
var app = getApp();
exports.default = Page({
  data: {
    val: 0
  },
  onLoad: function onLoad() {
    var _this = this;
    var openid = wx.getStorageSync("openid");
    _this.setData({
      openid: openid
    });
  },
  input: function input(e) {
    var _this = this;
    _this.setData({
      val: e.detail.value
    });
  },
  pay: function pay(e) {
    var _this = this;
    var val = parseInt(e.detail.value.val);
    if (val > 0) {
      wx.request({
        url: app.globalData.host,
        data: {
          mod: "record",
          act: "recharge",
          ssid: _this.data.openid,
          mid: val
        },
        method: "post", // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        success: function success(res) {
          // success
          //console.log(res);
          var ret = res.data.ret;
          if (ret != -1) {
            var oid = res.data.info.oid;
            var amount = res.data.info.amount;
            var openid = _this.data.openid;
            wx.request({
              url: app.globalData.WxHost + "ssid=" + openid + "&mid=" + oid + "&money=" + amount,
              success: function success(response) {
                //console.log(response);
                wx.requestPayment({
                  appId: response.data.appId,
                  nonceStr: response.data.nonceStr,
                  timeStamp: response.data.timeStamp,
                  package: response.data.package,
                  signType: response.data.signType,
                  paySign: response.data.paySign,
                  success: function success(result) {
                    //console.log(result);
                    _this.setData({
                      mid: ""
                    }, function () {
                      wx.showToast({
                        title: "购买成功！",
                        icon: "none",
                        duration: 1000,
                        mask: false
                      });
                    });
                  },
                  fail: function fail(result) {
                    //console.log(result)
                    wx.showToast({
                      title: "支付失败！",
                      icon: "none",
                      duration: 1000,
                      mask: false
                    });
                  },
                  complete: function complete() {
                    // complete
                  }
                });
              }
            });
          } else {
            wx.showToast({
              title: res.data.txt,
              icon: "none",
              duration: 1000,
              mask: false
            });
          }
        }
      });
    } else {
      wx.showToast({
        title: "充值积分不正确！",
        icon: "none",
        duration: 1000,
        mask: false
      });
    }
  }
});